package com.htthorizon.model.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.convert.NumberChineseFormatter;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.htthorizon.common.core.utils.Page;
import com.htthorizon.common.core.web.domain.AjaxResult;
import com.htthorizon.model.domain.FeedBackDto;
import com.htthorizon.model.domain.ModelInfo;
import com.htthorizon.model.domain.ModelMessage;
import com.htthorizon.model.domain.ModelUseCityDto;
import com.htthorizon.model.enums.MessageStatus;
import com.htthorizon.model.response.*;
import com.htthorizon.system.api.RemoteDataDictService;
import com.htthorizon.system.api.domain.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Brilliant
 */
@Service
public class StatisticsServiceImpl implements IStatisticsService{

    @Autowired
    private IModelInfoService modelInfoService;

    @Autowired
    private IModelMessageService modelMessageService;

    @Autowired
    private IModelUseService modelUseService;

    @Autowired
    private RemoteDataDictService remoteDataDictService;

    @Override
    public ModelStatusVO modelStatus() {
      List<ModelInfo> modelInfoList = modelInfoService.list();
        int total = modelInfoList.size();

        List<ModelInfo> using = modelInfoList.stream()
                .filter(modelInfo -> modelInfo.getModelStatus().equals("1"))
                .collect(Collectors.toList());

        List<ModelInfo> abandon = modelInfoList.stream()
                .filter(modelInfo -> modelInfo.getModelStatus().equals("2"))
                .collect(Collectors.toList());

        List<ModelInfo> develop = modelInfoList.stream()
                .filter(modelInfo -> modelInfo.getModelStatus().equals("3"))
                .collect(Collectors.toList());

        List<ModelInfo> unDevelop = modelInfoList.stream()
                .filter(modelInfo -> modelInfo.getModelStatus().equals("4"))
                .collect(Collectors.toList());

        ModelStatusVO modelStatusVO = new ModelStatusVO();
        modelStatusVO.setUsingPercent((double)(using.size())/total);
        modelStatusVO.setUsingCount(using.size());
        modelStatusVO.setDevelopPercent((double)(develop.size())/total);
        modelStatusVO.setDevelopCount(develop.size());
        modelStatusVO.setAbandonPercent((double)(abandon.size())/total);
        modelStatusVO.setAbandonCount(abandon.size());
        modelStatusVO.setUnDevelopPercent((double)(unDevelop.size())/total);
        modelStatusVO.setUnDevelopCount(unDevelop.size());

        return modelStatusVO;
    }

    @Override
    public SpecialtyVo specialty() {
        AjaxResult specialityCode = remoteDataDictService.dictType("speciality_code");
        List<LinkedHashMap<String,Object>> dictData = (List<LinkedHashMap<String,Object>>)specialityCode.get("data");
        List<ModelInfo> modelInfoList = modelInfoService.list();
        int total= modelInfoList.size();
        List<SpecialtyNode> list = new ArrayList<>();
        for (LinkedHashMap<String,Object> sysDictData :dictData) {
            SpecialtyNode specialtyNode = new SpecialtyNode();
            specialtyNode.setName(sysDictData.get("dictLabel").toString());
            String dictValue = sysDictData.get("dictValue").toString();
            specialtyNode.setType(dictValue);
            List<ModelInfo> marketing = modelInfoList.stream()
                    .filter(modelInfo -> modelInfo.getSpecialityCode().equals(dictValue))
                    .collect(Collectors.toList());
            specialtyNode.setPercent(marketing.size()/(double)total);
            specialtyNode.setCount(marketing.size());
            list.add(specialtyNode);
        }

        SpecialtyVo specialtyVo = new SpecialtyVo();
        specialtyVo.setList(list);
        return specialtyVo;
    }

    @Override
    public KeyIndicatorVo keyIndicator() {

        KeyIndicatorVo keyIndicatorVo = new KeyIndicatorVo();

        List<ModelInfo> modelInfoList = modelInfoService.list();

        int modelCount= modelInfoList.size();

        keyIndicatorVo.setModelCount(modelCount);

        keyIndicatorVo.setSpecialtyCount(5);

         int count =  modelUseService.modelException();

         int count1= modelUseService.modelBefore();

         keyIndicatorVo.setExceptionNum(count);
         keyIndicatorVo.setModelBefore(count1);

        return keyIndicatorVo;
    }

    @Override
    public FeedBackVo feedback() {

        FeedBackVo feedBackVo = new FeedBackVo();
       List<FeedBackNode> feedBack= new ArrayList<>();

        DateTime date = DateUtil.date();
        DateTime lastMonth1 = DateUtil.offset(date, DateField.MONTH, -1);
        FeedBackNode feedBackNode1 = this.getFeedBackNode(date, lastMonth1);
        feedBack.add(feedBackNode1);

        DateTime lastMonth2 = DateUtil.offset(lastMonth1, DateField.MONTH, -1);
        FeedBackNode feedBackNode2 = this.getFeedBackNode(lastMonth1, lastMonth2);
        feedBack.add(feedBackNode2);

        DateTime lastMonth3 = DateUtil.offset(lastMonth2, DateField.MONTH, -1);
        FeedBackNode feedBackNode3 = this.getFeedBackNode(lastMonth2, lastMonth3);
        feedBack.add(feedBackNode3);

        DateTime lastMonth4 = DateUtil.offset(lastMonth3, DateField.MONTH, -1);
        FeedBackNode feedBackNode4 = this.getFeedBackNode(lastMonth3, lastMonth4);
        feedBack.add(feedBackNode4);

        DateTime lastMonth5 = DateUtil.offset(lastMonth4, DateField.MONTH, -1);
        FeedBackNode feedBackNode5 = this.getFeedBackNode(lastMonth4, lastMonth5);
        feedBack.add(feedBackNode5);

        DateTime lastMonth6 = DateUtil.offset(lastMonth5, DateField.MONTH, -1);
        FeedBackNode feedBackNode6 = this.getFeedBackNode(lastMonth5, lastMonth6);
        feedBack.add(feedBackNode6);

        DateTime lastMonth7 = DateUtil.offset(lastMonth6, DateField.MONTH, -1);
        FeedBackNode feedBackNode7 = this.getFeedBackNode(lastMonth6, lastMonth7);
        feedBack.add(feedBackNode7);

        DateTime lastMonth8 = DateUtil.offset(lastMonth7, DateField.MONTH, -1);
        FeedBackNode feedBackNode8 = this.getFeedBackNode(lastMonth7, lastMonth8);
        feedBack.add(feedBackNode8);

        DateTime lastMonth9 = DateUtil.offset(lastMonth8, DateField.MONTH, -1);
        FeedBackNode feedBackNode9 = this.getFeedBackNode(lastMonth8, lastMonth9);
        feedBack.add(feedBackNode9);

        DateTime lastMonth10 = DateUtil.offset(lastMonth9, DateField.MONTH, -1);
        FeedBackNode feedBackNode10 = this.getFeedBackNode(lastMonth9, lastMonth10);
        feedBack.add(feedBackNode10);

        DateTime lastMonth11 = DateUtil.offset(lastMonth10, DateField.MONTH, -1);
        FeedBackNode feedBackNode11 = this.getFeedBackNode(lastMonth10, lastMonth11);
        feedBack.add(feedBackNode11);

        DateTime lastMonth12 = DateUtil.offset(lastMonth11, DateField.MONTH, -1);
        FeedBackNode feedBackNode12 = this.getFeedBackNode(lastMonth11, lastMonth12);
        feedBack.add(feedBackNode12);

        feedBackVo.setNodes(feedBack);

        return feedBackVo;
    }

    @Override
    public ModelTypeVo modelType() {
        ModelTypeVo modelTypeVo = new ModelTypeVo();

        ArrayList<ModelTypeNode> modelType = new ArrayList<>();

        DateTime date = DateUtil.date();
        DateTime lastMonth1 = DateUtil.offset(date, DateField.MONTH, -1);
        ModelTypeNode modelTypeNode1 = this.getModelTypeNode(date, lastMonth1);
        modelType.add(modelTypeNode1);

        DateTime lastMonth2 = DateUtil.offset(lastMonth1, DateField.MONTH, -1);
        ModelTypeNode modelTypeNode2 = this.getModelTypeNode(lastMonth1, lastMonth2);
        modelType.add(modelTypeNode2);

        DateTime lastMonth3 = DateUtil.offset(lastMonth2, DateField.MONTH, -1);
        ModelTypeNode modelTypeNode3 = this.getModelTypeNode(lastMonth2, lastMonth3);
        modelType.add(modelTypeNode3);

        DateTime lastMonth4 = DateUtil.offset(lastMonth3, DateField.MONTH, -1);
        ModelTypeNode modelTypeNode4 = this.getModelTypeNode(lastMonth3, lastMonth4);
        modelType.add(modelTypeNode4);

        DateTime lastMonth5 = DateUtil.offset(lastMonth4, DateField.MONTH, -1);
        ModelTypeNode modelTypeNode5 = this.getModelTypeNode(lastMonth4, lastMonth5);
        modelType.add(modelTypeNode5);

        DateTime lastMonth6 = DateUtil.offset(lastMonth5, DateField.MONTH, -1);
        ModelTypeNode modelTypeNode6 = this.getModelTypeNode(lastMonth5, lastMonth6);
        modelType.add(modelTypeNode6);

        modelTypeVo.setModeTypeList(modelType);

        return modelTypeVo;
    }

    @Override
    public ModelUseVo modelUse(Integer specType,Integer page,Integer pageSize) {
        ModelUseVo modelUseVo = new ModelUseVo();
        List<ModelUseDto> list= modelUseService.getModelUse(specType,page,pageSize);
       Integer count= modelUseService.getModelUseCount(specType);
        Page<ModelUseDto> objectPage = new Page<>(page,pageSize);
        objectPage.setRecords(list);
        objectPage.setTotal(count);
        modelUseVo.setPage(objectPage);
        return modelUseVo;
    }

    @Override
    public ModelUseCityVo modelUseCityVo() {
        ModelUseCityVo modelUseCityVo = new ModelUseCityVo();
        Map<String, Integer> map=  modelUseService.getModelUseCity();
        modelUseCityVo.setMap(map);
        return modelUseCityVo;
    }

    @Override
    public FeedBackVo feedback2() {

        return null;
    }

    @Override
    public ModelTypeVo modelType2() {
        List<FeedBackDto> feedBackDtoList = modelUseService.getFeedBack2();
        ModelTypeVo modelTypeVo = new ModelTypeVo();
        Map<String, List<FeedBackDto>> collect = feedBackDtoList.stream().collect(Collectors.groupingBy(FeedBackDto::getDate));
        List<ModelTypeNode> list = new ArrayList<>();
        for (String s:collect.keySet()){
            ModelTypeNode modelTypeNode = new ModelTypeNode();
            modelTypeNode.setMonth(s);
//            DateTime dateTime = DateTime.of(s, "yyyyMM");
//
//            int field = dateTime.getField(DateField.MONTH);
//            int field1 = dateTime.getField(DateField.YEAR);
//
//            String date = NumberChineseFormatter.formatThousand(field+1, false);
//            modelTypeNode.setMonth(date+"月");
//            if (date.equals("零")){
//                modelTypeNode.setMonth("十二月");
//            }
            List<FeedBackDto> feedBackDtos = collect.get(s);
            for (FeedBackDto feedBackDto:feedBackDtos){
                if (feedBackDto.getType().equals("预警类")){
                    modelTypeNode.setAlarm(feedBackDto.getData());
                }
                if (feedBackDto.getType().equals("疑点类")){
                    modelTypeNode.setQuestion(feedBackDto.getData());
                }
                if (feedBackDto.getType().equals("查询类")){
                    modelTypeNode.setQuery(feedBackDto.getData());
                }
            }
            list.add(modelTypeNode);
        }
        modelTypeVo.setModeTypeList(list);
        return modelTypeVo;
    }

    @Override
    public ModelUseCityVo modelUseCityVo2() {
        List<ModelUseCityDto> modelUseCityDtos = modelUseService.modelUseCityVo2();
        Map<String, Integer> map = new LinkedHashMap<>();
        modelUseCityDtos.forEach(modelUseCityDto -> {
            map.put(modelUseCityDto.getDept(),modelUseCityDto.getData());
        });
        ModelUseCityVo modelUseCityVo = new ModelUseCityVo();
        modelUseCityVo.setMap(map);
        return modelUseCityVo;
    }
    @Deprecated
    private ModelTypeNode getModelTypeNode(DateTime date,DateTime lastMonth){
        ModelTypeNode modelTypeNode = new ModelTypeNode();
        List<ModelInfo> list=modelInfoService.queryTimeLimit(date,lastMonth);
        List<ModelInfo> query = list.stream()
                .filter(modelInfo -> modelInfo.getAlarmType().equals(1)).collect(Collectors.toList());

        List<ModelInfo> alarm = list.stream()
                .filter(modelInfo -> modelInfo.getAlarmType().equals(2)).collect(Collectors.toList());

        List<ModelInfo> question = list.stream()
                .filter(modelInfo -> modelInfo.getAlarmType().equals(3)).collect(Collectors.toList());

//                modelTypeNode.setQuery(query.size());
//                modelTypeNode.setAlarm(alarm.size());
//                modelTypeNode.setQuestion(question.size());


        int field = date.getField(DateField.MONTH);

        String s = NumberChineseFormatter.formatThousand(field+1, false);
        modelTypeNode.setMonth(s+"月");
        if (s.equals("零")){
            modelTypeNode.setMonth("十二月");
        }
        return modelTypeNode;
    }

    public static void main(String[] args) {
        String date="202401";
        DateTime dateTime = DateTime.of(date, "yyyyMM");
        System.out.println(dateTime);
        int field = dateTime.getField(DateField.MONTH);
        int year = dateTime.getField(DateField.YEAR);
        System.out.println(field);
        System.out.println(year);
        String s = NumberChineseFormatter.formatThousand(field+1, false);

    }
    private FeedBackNode getFeedBackNode(DateTime date,DateTime lastMonth){
        FeedBackNode feedBackNode = new FeedBackNode();
        List<ModelMessage> list= modelMessageService.queryTimeLimit(date,lastMonth);

        List<ModelMessage> replied = list.stream()
                .filter(modelMessage -> modelMessage.getQuestionStatus()
                        .equals(MessageStatus.REPLED.getValue()))
                .collect(Collectors.toList());
        feedBackNode.setReplied(replied.size());

        List<ModelMessage> data = list.stream()
                .filter(modelMessage -> modelMessage.getQuestionType()
                        .equals(0))
                .collect(Collectors.toList());
        feedBackNode.setQuestionType(data.size());

        List<ModelMessage> business = list.stream()
                .filter(modelMessage -> modelMessage.getQuestionType()
                        .equals(1))
                .collect(Collectors.toList());
        feedBackNode.setBusinessType(business.size());

        List<ModelMessage> comprehensive = list.stream()
                .filter(modelMessage -> modelMessage.getQuestionType()
                        .equals(2))
                .collect(Collectors.toList());
        feedBackNode.setComprehensiveType(comprehensive.size());

        int field = lastMonth.getField(DateField.MONTH);
        int year = lastMonth.getField(DateField.YEAR);
        String s = "0"+String.valueOf(field+1);
        String s1 = String.valueOf(year) + s;
        feedBackNode.setMonth(Integer.valueOf(s1));
//        if (field==0){
//            feedBackNode.setMonth(Integer.valueOf(year+"12"));
//        }

        return feedBackNode;
    }

//    public static void main(String[] args) {
////        DateTime date = DateUtil.date();
////        System.out.println(date);
////        DateTime offset = DateUtil.offset(date, DateField.MONTH, -1);
////        System.out.println(offset);
//        List<ModelUseDto> list=new ArrayList<>();
//        ModelUseDto modelUseDto = new ModelUseDto();
//        modelUseDto.setModelName("Test1");
//        modelUseDto.setSpecialityCode("1");
//        modelUseDto.setDictLabel("物资");
//        modelUseDto.setRecordNum(1001);
//        modelUseDto.setMom(1.23D);
//        modelUseDto.setYoy(1.78D);
//
//
//        ModelUseDto modelUseDto2 = new ModelUseDto();
//        modelUseDto2.setModelName("Test1");
//        modelUseDto2.setSpecialityCode("1");
//        modelUseDto2.setDictLabel("物资");
//        modelUseDto2.setRecordNum(1001);
//        modelUseDto2.setMom(1.23D);
//        modelUseDto2.setYoy(1.78D);
//
//
//        ModelUseDto modelUseDto3 = new ModelUseDto();
//        modelUseDto3.setModelName("Test1");
//        modelUseDto3.setSpecialityCode("1");
//        modelUseDto3.setDictLabel("物资");
//        modelUseDto3.setRecordNum(1001);
//        modelUseDto3.setMom(1.23D);
//        modelUseDto3.setYoy(1.78D);
//
//        ModelUseDto modelUseDto4 = new ModelUseDto();
//        modelUseDto4.setModelName("Test2");
//        modelUseDto4.setSpecialityCode("1");
//        modelUseDto4.setDictLabel("物资1");
//        modelUseDto4.setRecordNum(1001);
//        modelUseDto4.setMom(1.23D);
//        modelUseDto4.setYoy(1.78D);
//
//        list.add(modelUseDto);
//        list.add(modelUseDto2);
//        list.add(modelUseDto3);
//        list.add(modelUseDto4);
//
//        Map<String, List<ModelUseDto>> collect = list.stream().collect(Collectors.groupingBy((ModelUseDto::getDictLabel)));
//        for (Map.Entry<String, List<ModelUseDto>> entry : collect.entrySet()) {
//            System.out.println(entry.getKey());
//            System.out.println(entry.getValue());
//        }
//
//
//    }


}
